package com.eudon.genius.api.core.parser.component;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Controller分析器
 * 负责识别和分析Spring Controller类
 *
 * @author eudon
 * @since 2025/9/25
 */
@Deprecated
@Slf4j
public class ControllerAnalyzer {

    /**
     * Spring Controller相关注解
     */
    private static final List<String> CONTROLLER_ANNOTATIONS = Arrays.asList(
            "RestController",
            "Controller",
            "org.springframework.web.bind.annotation.RestController",
            "org.springframework.web.bind.annotation.Controller",
            "org.springframework.stereotype.Controller"
    );

    /**
     * 判断编译单元是否包含Controller类
     *
     * @param compilationUnit 编译单元
     * @return 是否为Controller
     */
    public boolean isController(CompilationUnit compilationUnit) {
        return compilationUnit.findAll(ClassOrInterfaceDeclaration.class)
                .stream()
                .anyMatch(this::isControllerClass);
    }

    /**
     * 获取编译单元中的Controller类
     *
     * @param compilationUnit 编译单元
     * @return Controller类列表
     */
    public List<ClassOrInterfaceDeclaration> getControllerClasses(CompilationUnit compilationUnit) {
        return compilationUnit.findAll(ClassOrInterfaceDeclaration.class)
                .stream()
                .filter(this::isControllerClass)
                .collect(Collectors.toList());
    }

    /**
     * 判断类是否为Controller类
     *
     * @param classDeclaration 类声明
     * @return 是否为Controller类
     */
    public boolean isControllerClass(ClassOrInterfaceDeclaration classDeclaration) {
        // 跳过接口和抽象类
        if (classDeclaration.isInterface() || classDeclaration.isAbstract()) {
            return false;
        }

        // 检查类上的注解
        return classDeclaration.getAnnotations()
                .stream()
                .anyMatch(this::isControllerAnnotation);
    }

    /**
     * 判断注解是否为Controller相关注解
     *
     * @param annotation 注解表达式
     * @return 是否为Controller注解
     */
    private boolean isControllerAnnotation(AnnotationExpr annotation) {
        String annotationName = annotation.getNameAsString();

        return CONTROLLER_ANNOTATIONS.stream()
                .anyMatch(controllerAnnotation ->
                        controllerAnnotation.equals(annotationName) ||
                                controllerAnnotation.endsWith("." + annotationName)
                );
    }

    /**
     * 获取Controller类的完整类名
     *
     * @param compilationUnit  编译单元
     * @param classDeclaration 类声明
     * @return 完整类名
     */
    public String getFullClassName(CompilationUnit compilationUnit, ClassOrInterfaceDeclaration classDeclaration) {
        Optional<String> packageName = compilationUnit.getPackageDeclaration()
                .map(pd -> pd.getNameAsString());

        String className = classDeclaration.getNameAsString();

        if (packageName.isPresent()) {
            return packageName.get() + "." + className;
        } else {
            return className;
        }
    }

    /**
     * 获取Controller类的简单类名
     *
     * @param classDeclaration 类声明
     * @return 简单类名
     */
    public String getSimpleClassName(ClassOrInterfaceDeclaration classDeclaration) {
        return classDeclaration.getNameAsString();
    }
}